
# coding: utf-8

# ### 直接调用XGBOOST
# #### 1，直接读取数据——Dmatrix
# #### 2，设置参数
# #### 3,模型训练
# #### 4，预测

# ### 导入必要的工具包

# ### 一、数据读取

# In[2]:


import xgboost as xgb
from sklearn.metrics import accuracy_score


# In[3]:


dtrain=xgb.DMatrix('D:/AI/homework/week3/code/data/RentListingInquries_FE_train.bin')
dtest=xgb.DMatrix('D:/AI/homework/week3/code/data/RentListingInquries_FE_test.bin')


# In[4]:


dtrain.num_col()


# In[5]:


dtrain.num_row()


# In[6]:


dtest.num_col()


# In[7]:


dtest.num_row()


# In[8]:


dtrain.num_col()


# In[9]:


dtrain.get_label()


# In[10]:


dtest.get_label()


# In[11]:


dtrain.feature_names


# In[12]:


dtest.feature_names


# In[13]:


dtrain.feature_types


# ### 二、训练参数设置

# In[14]:


param = {'max_depth':34, 'eta':0.8, 'silent':1, 'objective':'multi:softmax' ,'num_class':3}


# ### 三、训练模型

# In[15]:


num_boost_round=100

bst = xgb.train(param, dtrain, num_boost_round)


# In[16]:


train_preds = bst.predict(dtrain)
y_train = dtrain.get_label()
train_accuracy = accuracy_score(y_train, train_preds)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))


# ### 四、预测

# In[17]:


preds=bst.predict(dtest)
print(preds)


# ### 五、模型可视化

# In[18]:


from matplotlib import pyplot
import graphviz
xgb.plot_tree(bst,num_trees=0, rankdir= 'LR' )
xgb.to_graphviz(bst,num_trees=0)
pyplot.show()


# ### 六、用XGBOOST内嵌的CV交叉验证得到合适的n_estimators
# 

# In[34]:


early_stopping_rounds=20
n_estimators=1000
cv_result=xgb.cv(param,dtrain,num_boost_round=n_estimators,folds =5,metrics=(),
                 early_stopping_rounds=early_stopping_rounds)
cv_result


# ### 七、用最佳的n_esimators重新训练，得到的模型评估下原始数据集的精确率

# In[35]:


n_estimators=cv_result.shape[0]
n_estimators
bst=xgb.train(param,dtrain,n_estimators)
preds=bst.predict(dtrain)
y_train=dtrain.get_label()
train_accuracy=accuracy_score(y_train,preds)
train_accuracy


# ### 八、特征排序并且进行特征选择

# In[36]:


from xgboost import plot_importance
plot_importance(bst)
pyplot.show()


# In[37]:


test_means = cv_result['test-merror-mean']
test_stds = cv_result['test-merror-std'] 

x_axis = range(0, cv_result.shape[0])
        
pyplot.errorbar(x_axis, test_means, yerr=test_stds ,label='Test')

pyplot.title("XGBoost n_estimators vs Log Loss")
pyplot.xlabel( 'n_estimators' )
pyplot.ylabel( 'merror' )

pyplot.show()

